package com.jeesite.modules.app;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.config.OSSUtils;

/**
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/app/")
public class AppUploadFileController {
	@RequestMapping(value="uploadfile")
	public void uploadfile(@RequestParam(value="file") MultipartFile[] nodeFile, HttpServletRequest request,HttpServletResponse response){
		if(nodeFile==null){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "文件未发现");
			return;
		}
		int a=nodeFile.length;
		if(a==0){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "文件未发现");
			return;
		}
		OSSUtils ossUtils=new OSSUtils();
		try {
			List<String> files=ossUtils.uploadFile(nodeFile);
			JSONArray parseArray = JSONObject.parseArray(JsonMapper.toJson(files));
			AppController.renderSuccessObject(response, parseArray,MessageCode.SUCCESS_ACK);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}		
	}
	@RequestMapping(value="moveFile")
	public void deleteObject(HttpServletRequest request,HttpServletResponse response){
		String fileKey=request.getParameter("filePath");
		if(StringUtils.isBlank(fileKey)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "文件未发现");
			return;
		}
		OSSUtils ossUtils=new OSSUtils();		
		String message=ossUtils.deleteObject(fileKey);
		AppController.renderSuccessObject(response, message, MessageCode.SUCCESS_ACK);
	}
}
